<!--
 * @description  : 市管-日计划审批 统一待办审批跳转页-辽宁
 * @descriptionDetail: 市管-日计划审批 统一待办审批跳转页-辽宁
 * @copyright    : 浙江烟草
 * @author       : bln
 * @create       : 2023-11-16 17:57:44
-->
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
		<!-- #endif -->
		<view class="content">
			<u-form ref="uForm" v-model="baseInfo" label-position="top" label-width="auto">
				<form-section title="基础信息"></form-section>
				<u-form-item label="计划日期">
					<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.planDate"></u-input>
				</u-form-item>
				<u-form-item label="计划类型">
					<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.dayPlanTypeStr"></u-input>
				</u-form-item>
				<u-form-item label="执行人">
					<view class="f-ctn">
						<text class="sub-title" v-for="itm in baseInfo.executorVOList" :key="itm.personUuid">
							{{ itm.personName }}
						</text>
					</view>
				</u-form-item>
				<u-collapse ref="cons" @change="changeHeight()">
					<u-collapse-item>
						<template #title>
							<u-row>
								<u-col span="12" style="display: flex">
									<view style="
											width: 8rpx;
											height: 32rpx;
											background: #0079f2;
											opacity: 1;
											margin-left: -5px;
											border-radius: 4rpx;
											float: left;
											margin-right: 16rpx;
										"></view>
									<view
										style="height: 36rpx; line-height: 36rpx; text-align: center; float: left; font-weight: bold; font-size: 16px">
										审批列表
									</view>
								</u-col>
							</u-row>
						</template>
						<scroll-view :scroll-y="false" :scroll-x="true"
							:style="{ height: scrollRealHeight, marginTop: '5px', paddingBottom:'10px'}">
							<u-table class="tb-lay" ref="tbRef">
								<u-tr>
									<u-th width="4%">序号</u-th>
									<u-th width="12%">许可证号</u-th>
									<u-th width="18%">企业【字号】名称</u-th>
									<u-th width="10%">持证人</u-th>
									<u-th width="33%">地址</u-th>
									<u-th width="13%">所属机构</u-th>
									<u-th width="10%">任务主体</u-th>
								</u-tr>
								<u-tr v-for="(it, index) in dataSource" :key="'ds' + index">
									<u-td width="4%">{{ index + 1 }}</u-td>
									<u-td width="12%">{{ it.licNo || '&nbsp;' }}</u-td>
									<u-td width="18%">{{ it.companyName || '&nbsp;' }}</u-td>
									<u-td width="10%">{{ it.managerName || '&nbsp;' }}</u-td>
									<u-td width="33%">{{ it.businessAddr || '&nbsp;' }}</u-td>
									<u-td width="13%">{{ it.orgName || '&nbsp;' }}</u-td>
									<u-td width="10%">{{ getDicNameByKeyCode($u.dic.DIC_KEYS.M_TASK_SUBJECT, it.taskSubject) || '&nbsp;' }}</u-td>
								</u-tr>
							</u-table>
						</scroll-view>
					</u-collapse-item>
				</u-collapse>
				<u-gap v-if="!approvalDisabled && !ifSub" :marginRight="getGapMarginRowNumber()"
					:marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height"
					:bg-color="$theme.cm_gap_color"></u-gap>
				<form-section v-if="!approvalDisabled && !ifSub" title="审批信息"></form-section>
				<u-form-item v-if="!approvalDisabled && !ifSub" label="审批意见">
					<u-input type="textarea" height="100" :auto-height="true" placeholder="请输入" v-model="approveMemo" />
				</u-form-item>
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="审批历史"></form-section>
				<u-form-item label="" class="u-m-l-12">
					<flow-his-no-doc :bizUuid="prvParam.bizUuid" processDefKey="dayPlanApproval"
						:endTitle="endTitle"></flow-his-no-doc>
				</u-form-item>
			</u-form>
		</view>
		<view class="submit-btns btn-other-cls" v-if="!approvalDisabled && !ifSub && !approveDis">
			<u-button class="u-btn" type="error" @click="unSubmit">退回</u-button>
			<u-button class="u-btn" type="primary" @click="submit">审批通过</u-button>
		</view>
	</view>
</template>

<script>
	import DicChoice from '@/components/dic_choice.vue';
	import DicInput from '@/components/dic_input.vue';
	import formSection from '@/components/form-section.vue';
	import checkService from '@/service/check/check.service.js';
	import {
		M_DAY_PLAN_STATUS
	} from '@/service/check/mapping/enum.js';
	import FlowHisNoDoc from '@/components/flow-his-no-doc.vue';
	export default {
		components: {
			formSection,
			FlowHisNoDoc,
			DicChoice,
			DicInput,
		},
		onLoad(opt) {
			this.prvParam = this.getRouteParam() || {};
			this.initData();
		},
		data() {
			return {
				dicKeyWords: '',
				dicCurrentSelect: '',
				dicShow: false,
				dicType: '',
				prvParam: {},
				baseInfo: {},
				dataSource: [],
				scrollRealHeight: '50px',
				bean: {},
				approvalDisabled: false,
				approveMemo: '同意',
				noCheckPersonNodes: '03',
				approvePersonUuid: '',
				ifSub: false,
				approveDis: true,
				endTitle: '',
			};
		},
		methods: {
			changeHeight() {
				this.$nextTick(() => {
					this.$refs.cons.init();
				});
			},
			updateTableHeight() {
				setTimeout(() => {
					this.scrollRealHeight = this.$refs.tbRef.$el.clientHeight + 5 + 'px';
				}, 100);
			},
			getCurrStatus() {
				checkService.getFlowNodeStep({
					bizUuid: this.prvParam.bizUuid,
					processDefKey: 'dayPlanApproval'
				}).then((res) => {
					if (res.success) {
						let step;
						if (res.bean) {
							step = res.bean.step;
						}
						this.approveDis = false;
						if (step == 'view') {
							this.approvalDisabled = true;
						} else if (step == 'edit') {
							this.ifSub = true;
						}
					} else {
						console.log(res.msg);
					}
				});
			},
			initData() {
				this.approvePersonUuid = this.$store.state.userInfo.user.personUuid;
				this.$modal.showLoading('加载中...');
				checkService
					.getDayPlan(this.prvParam.bizUuid)
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success) {
							this.bean = res.bean;
							this.getCurrStatus();
							this.endTitle = this.bean.status == M_DAY_PLAN_STATUS.TERMINATION ? '结束' : '...';
							this.baseInfo.planDate = res.bean.planDate;
							this.baseInfo.dayPlanType = res.bean.dayPlanType;
							this.baseInfo.executorVOList = res.bean.executorList;
							this.dataSource = res.bean.taskPoolQueryVOS || [];
							this.updateTableHeight();
							this.baseInfo.dayPlanTypeStr = this.getDicNameByKeyCode(
								this.$u.dic.DIC_KEYS.M_DAY_PLAN_TYPE,
								this.baseInfo.dayPlanType
							);
						} else {
							this.$modal.alert(res.msg, () => {
								this.error();
							});
						}
					})
					.catch((e) => {
						this.$modal.hideLoading();
						this.$modal.alert('加载失败' + e, () => {
							this.error();
						});
					});
			},
			getDicNameByKeyCode(key, code) {
				return this.$u.dic.getDicNameByKeyCode(key, code);
			},
			toSelectPerson() {
				this.$u.route({
					url: '/pages/commonPage/flowUserSelectComponent',
					params: {
						flowFormCode: 'dayPlanApproval', // 日计划审批的gfcode
						docInsUuid: this.prvParam.bizUuid,
					},
				});
			},
			getGapMarginRowNumber() {
				return '-' + this.$theme.cm_content_paddng_row.replace('rpx', '');
			},
			submit() {
				if (this.$u.test.isEmpty(this.approveMemo) || this.approveMemo == '不同意') {
					this.approveMemo = '同意';
				}
				this.$modal.showLoading('请稍后...');
				checkService
					.getFlowIsFinish({
						bizUuid: this.prvParam.bizUuid,
						noCheckPersonNodes: this.noCheckPersonNodes,
					})
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success) {
							if (!res.bean) {
								// 是否流程最后一步
								this.toSelectPerson();
							} else {
								this.realSubmit();
							}
						} else {
							this.$u.toast(res.msg);
						}
					})
					.catch((e) => {
						this.$modal.hideLoading();
						this.$u.toast('请求失败' + e);
					});
			},
			realSubmit() {
				this.$modal.showLoading('正在提交...');
				let parms = {
					approveResult: '01',
					nextPersonUuids: '',
					bizUuid: this.prvParam.bizUuid,
					approveMemo: this.approveMemo,
					processDefKey: this.prvParam.gfCode,
					approvePersonUuid: this.approvePersonUuid,
				};
				checkService
					.approSubmitDayPlan(parms)
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success) {
							this.backPrev({
								paramKey: 'refresh',
								param: true
							});
						} else {
							this.$u.toast('审批失败！');
						}
					})
					.catch((err) => {
						this.$modal.hideLoading();
						this.$u.toast('提交异常' + err);
					});
			},
			unSubmit() {
				if (this.$u.test.isEmpty(this.approveMemo) || this.approveMemo == '同意') {
					this.approveMemo = '不同意';
				}
				let parms = {
					bizUuid: this.prvParam.bizUuid,
					nextPersonUuids: '',
					approveMemo: this.approveMemo,
					approveResult: '02', // 退回
					processDefKey: this.prvParam.gfCode,
					approvePersonUuid: this.approvePersonUuid,
				};
				this.$modal.showLoading('请稍后...');
				checkService
					.approSubmitDayPlan(parms)
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success) {
							this.backPrev({
								paramKey: 'refresh',
								param: true
							});
						} else {
							this.$u.toast(res.msg);
						}
					})
					.catch((e) => {
						this.$modal.hideLoading();
						this.$u.toast('提交失败' + e);
					});
			},
			error() {
				uni.navigateBack();
			},
		},
	};
</script>

<style scoped lang="scss">
	.page {
		padding: 0;
	}

	.sub-title {
		background: #f5f5f5;
		color: #989ea1;
		font-size: 24rpx;
		border-radius: 4px;
		margin-right: 10px;
		padding: 4px 8px;
	}

	.f-ctn {
		width: 100%;
	}

	.tb-lay {
		width: 1000px;
		margin-left: 1px;
		margin-right: 1px;
	}

	.content {
		padding-left: $cm-content-paddng-row;
		padding-right: $cm-content-paddng-row;
		padding-top: $cm-conent-padding-column;
		padding-bottom: 70px;
	}

	/deep/.u-td {
		height: auto;
	}
</style>